package com.tourex;

import java.util.ArrayList;
import java.util.List;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.tourex.C.bean.MyLocationInfo;
import com.tourex.thirdparty.volley.BitmapCache;
import com.tourex.utils.LogUtils;

@SuppressWarnings("deprecation")
public class App extends Application {

	private static App instance;
	public RequestQueue requestQueue;
	public ImageLoader imageLoader;
	public BitmapCache bitmapCache;

	public MyLocationInfo mli;

	public boolean requestingLocation = false;
	/** 开启应用后完成过定位操作 */
	public boolean hasLocation = false;

	private AMapLocation aMapLocation;// 用于判断定位超时
	private LocationManagerProxy aMapLocManager = null;
	private Handler handler = new Handler();

	@Override public void onCreate() {
		super.onCreate();

		instance = this;

		requestQueue = Volley.newRequestQueue(instance);
		bitmapCache = new BitmapCache();
		imageLoader = new ImageLoader(requestQueue, bitmapCache);

		mli = new MyLocationInfo();
		mli.cityCode = "488";
		getMyLatLngFromSp();

		aMapLocManager = LocationManagerProxy.getInstance(this);
		/*
		 * mAMapLocManager.setGpsEnable(false);//
		 * 1.0.2版本新增方法，设置true表示混合定位中包含gps定位，false表示纯网络定位，默认是true Location
		 * API定位采用GPS和网络混合定位方式
		 * ，第一个参数是定位provider，第二个参数时间最短是2000毫秒，第三个参数距离间隔单位是米，第四个参数是定位监听者
		 */
		aMapLocManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 2000, 10, aMapLocationListener);
		handler.postDelayed(stopLoc, 12000);// 设置超过12秒还没有定位到就停止定位

		CrashHandler.init(instance);
	}

	private AMapLocationListener aMapLocationListener = new AMapLocationListener() {
		@Override public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		@Override public void onProviderEnabled(String provider) {
		}

		@Override public void onProviderDisabled(String provider) {
		}

		@Override public void onLocationChanged(Location location) {
		}

		@Override public void onLocationChanged(AMapLocation location) {
			aMapLocation = location;// 判断超时机制
			Double geoLat = location.getLatitude();
			Double geoLng = location.getLongitude();
			String cityCode = "";
			String desc = "";
			Bundle locBundle = location.getExtras();
			if (locBundle != null) {
				cityCode = locBundle.getString("citycode");
				desc = locBundle.getString("desc");
			}
			String str = ("定位成功:(" + geoLng + "," + geoLat + ")" + "\n精    度    :" + location.getAccuracy() + "米" + "\n定位方式:"
					+ location.getProvider() + "\n定位时间:" + location.getTime() + "\n城市编码:" + cityCode + "\n位置描述:" + desc + "\n省:"
					+ location.getProvince() + "\n市:" + location.getCity() + "\n区(县):" + location.getDistrict() + "\n区域编码:" + location
					.getAdCode());

			LogUtils.logd(str);

			stopLocation();

			setMyLatLngToSp(geoLat, geoLng);
			setMliCityCode(str);
		}
	};

	private Runnable stopLoc = new Runnable() {
		@Override public void run() {
			if (aMapLocation == null) {
				// Toast.makeText(getApplicationContext(), "12秒内还没有定位成功，停止定位",
				// Toast.LENGTH_SHORT).show();
				LogUtils.logd("12秒内还没有定位成功，停止定位");
				stopLocation();// 销毁掉定位
			}
		}
	};

	private void stopLocation() {
		if (aMapLocManager != null) {
			aMapLocManager.removeUpdates(aMapLocationListener);
			aMapLocManager.destory();
		}
		aMapLocManager = null;

		hasLocation = true;
	}

	public void setMyLatLngToSp(double lat, double lng) {
		SharedPreferences sp = getSharedPreferences(C.sp.sp_path, Context.MODE_PRIVATE);
		sp.edit().putString(C.sp.my_lat, String.valueOf(lat)).commit();
		sp.edit().putString(C.sp.my_lng, String.valueOf(lng)).commit();
	}

	public void getMyLatLngFromSp() {
		SharedPreferences sp = getSharedPreferences(C.sp.sp_path, Context.MODE_PRIVATE);
		mli.lat = Double.parseDouble(sp.getString(C.sp.my_lat, "26.57"));
		mli.lng = Double.parseDouble(sp.getString(C.sp.my_lng, "106.71"));
	}

	private void setMliCityCode(String addrStr) {
		if (addrStr == null) {
			mli.cityCode = "488"; // default
			mli.address = "贵阳";
			mli.weatherCode = "101260101";
			mli.lat = 26.57;
			mli.lng = 106.71;
		} else if (addrStr.contains("毕节地区")) {
			mli.cityCode = "483";
			mli.address = "毕节地区";
			mli.weatherCode = "101260701";
		} else if (addrStr.contains("铜仁地区")) {
			mli.cityCode = "484";
			mli.address = "铜仁地区";
			mli.weatherCode = "101260601";
		} else if (addrStr.contains("安顺")) {
			mli.cityCode = "485";
			mli.address = "安顺";
			mli.weatherCode = "101260301";
		} else if (addrStr.contains("遵义")) {
			mli.cityCode = "486";
			mli.address = "遵义";
			mli.weatherCode = "101260201";
		} else if (addrStr.contains("六盘水")) {
			mli.cityCode = "487";
			mli.address = "六盘水";
			mli.weatherCode = "101260801";
		} else if (addrStr.contains("贵阳")) {
			mli.cityCode = "488";
			mli.address = "贵阳";
			mli.weatherCode = "101260101";
		} else if (addrStr.contains("黔东南州")) {
			mli.cityCode = "829";
			mli.address = "黔东南州";
			mli.weatherCode = "101260501";
		} else if (addrStr.contains("黔西南州")) {
			mli.cityCode = "830";
			mli.address = "黔西南州";
			mli.weatherCode = "101260901";
		} else if (addrStr.contains("贵安新区")) {
			mli.cityCode = "831";
			mli.address = "贵安新区";
			// ???
		} else if (addrStr.contains("黔南州")) {
			mli.cityCode = "832";
			mli.address = "黔南州";
			mli.weatherCode = "101260401";
		} else {
			mli.cityCode = "488"; // default
			mli.address = "贵阳";
			mli.weatherCode = "101260101";
			mli.lat = 26.57;
			mli.lng = 106.71;
		}
		for (MyLocationListener myLocationListener : myLocationListenerList) {
			myLocationListener.onLocation();
		}
	}

	public List<MyLocationListener> myLocationListenerList = new ArrayList<MyLocationListener>();

	public void registerLocationListener(MyLocationListener myLocationListener) {
		if (!myLocationListenerList.contains(myLocationListener)) {
			myLocationListenerList.add(myLocationListener);
		}
	}

	public void unRegisterLocationListener(MyLocationListener myLocationListener) {
		if (myLocationListenerList.contains(myLocationListener)) {
			myLocationListenerList.remove(myLocationListener);
		}
	}

	public interface MyLocationListener {
		void onLocation();
	}

	public static synchronized App i() {
		return instance;
	}

	@Override public void onTerminate() {
		super.onTerminate();
	}

	@Override public void onLowMemory() {
		super.onLowMemory();
	}
}
